<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-3-30
 * Time: 21:41
 */

namespace app\admin\controller;

use app\admin\model\User;
use think\Controller;
use think\facade\Session;

class login extends Controller
{
    public function index()
    {
        if (session('username')) {
            $this->redirect('/admin');
        }

        return $this->fetch('index/login');
    }

    public function dologin(){
        if(!captcha_check(trim($_POST['verify']))){
            $this->error('验证码输入错误！');
        }
        $username = $_POST['username'];
        $password = $_POST['password'];

        $user = User::get(['username'=>$username]);
        if($user){
            if($user->password == $password){
                session('user_id',$user->id);
                session('username',$user->username);
                session('before_ip',$user->ip);
                session('before_datetime',$user->update_time);
                $user->where('id',$user->id)->update(['ip'=>request()->ip(),'update_time'=>get_datetime()]);
                $this->success('登录成功');
            }else{
                $this->error('密码错误，请确认后重试！');
            }
        }else{
            $this->error('没有该用户信息，请确认后重试或注册新的帐号！');
        }
    }

    public function logout(){
        Session::clear();
        return $this->index();
    }
}